package v1

import (
	"github.com/gin-gonic/gin"
	"shopstar/api/v1/goods"
	"shopstar/api/v1/member"
	"shopstar/api/v1/order"
	"shopstar/api/v1/system"
	"shopstar/api/v1/test"
	"shopstar/core/router"
	"shopstar/middleware"
)

func InitRouter() {
	// 加载全局中间件
	router.Use(middleware.RequestLog(), middleware.Recover())
	router.Register(memberViper, systemViper, testApi, goodsApi, orderApi) // 注册路由
}

// memberViper  实现了 router中的 Router 方法
func memberViper(g *gin.Engine) {
	group := g.Group("member")
	{
		group.POST("/login", member.Login)
		group.POST("/register", member.Register)
		group.GET("/get_member", member.GetMember)
		group.GET("/u", func(ctx *gin.Context) {
			ctx.JSON(200, gin.H{
				"msg": "222",
			})
		})

		group.GET("/default_address", member.DefaultAddress)
	}

}

func systemViper(g *gin.Engine) {
	group := g.Group("system")
	{
		group.POST("/smscode", system.SmsCode)
		group.GET("/smscaptcha", system.Captcha)
	}

}

func goodsApi(g *gin.Engine) {
	group := g.Group("goods")
	{
		group.GET("attr", goods.AttributeList)
		group.GET("sku", goods.GetGoodsSku)
		group.GET("/attrs", goods.AttributeList)
		group.GET("/sku_tbdetail", goods.SkuTbDetail)
	}
}

func orderApi(g *gin.Engine) {
	groupCart := g.Group("cart")
	{
		groupCart.POST("/", order.AddCart)
		groupCart.GET("/", order.IndexCart)
	}
}

func testApi(g *gin.Engine) {
	group := g.Group("test")
	{
		group.GET("/cache_get", test.GetCache)
		group.GET("/cache_set", test.SetCache)
		group.POST("/val_sms_code",
			middleware.SmsCodeVerification(), // 中间件
			test.Validate_SMSCode,
		)
		group.POST("/val_captcha",
			middleware.CaptchaVerification(),
			test.Validate_Captcha,
		)
		group.POST("/valdata",
			middleware.CaptchaVerification(),
			middleware.SmsCodeVerification(),
			test.Validate,
		)
		group.GET("/jwt_gen", test.GenJwt)
		group.POST("/jwt_val", middleware.JwtAuth(), test.ValJwt)
		group.POST("/jwt_ref", middleware.JwtAuth(), test.RefJwt)
	}
}
